package com.aaa.car.controller;

import com.aaa.car.entity.Order;
import com.aaa.common.vo.OrderVo;
import com.aaa.car.service.OrderService;
import com.aaa.common.util.Result;
import com.aaa.common.util.ResultStatus;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Order)表控制层
 *
 * @author lei chu
 * @since 2021-02-21 14:19:05
 */
@RestController
@RequestMapping("order")
@ApiOperation("order控制器")
public class OrderController {
    /**
     * 服务对象
     */
    @Resource
    private OrderService orderService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("query/{id}")
    public Result<Order> selectOne(@PathVariable("id") Integer id) {
        return new Result<>(ResultStatus.SUCCESS.getCode(),
                ResultStatus.SUCCESS.getMessage(),
                this.orderService.queryById(id));
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @param pageNum  当前页数
     * @param pageSize 每页条数
     * @param order    实例对象
     * @return 分页数据和信息
     */
    @PostMapping("queryAll")
    public Result<PageInfo<Order>> queryAll(Integer pageNum, Integer pageSize, @RequestBody Order order) {
        return new Result<>(ResultStatus.SUCCESS.getCode(),
                ResultStatus.SUCCESS.getMessage(),
                this.orderService.queryAll(pageNum, pageSize, order));
    }

    @PostMapping("queryOrderVo")
    public Result<PageInfo<OrderVo>> queryOrderVo(Integer pageNum, Integer pageSize, @RequestBody OrderVo orderVo) {
        return new Result<>(ResultStatus.SUCCESS.getCode(),
                ResultStatus.SUCCESS.getMessage(),
                this.orderService.queryOrderVo(pageNum, pageSize, orderVo));
    }

    /**
     * 新增数据
     *
     * @param ordervo 实例对象
     * @return 实例对象
     */
    @PostMapping("insert")
    public Result<OrderVo> insert(@RequestBody OrderVo ordervo) {
        return new Result<>(ResultStatus.SUCCESS.getCode(),
                ResultStatus.SUCCESS.getMessage(),
                this.orderService.insert(ordervo));
    }

    /**
     * 修改数据
     *
     * @param order 实例对象
     * @return 实例对象
     */
    @PutMapping("update")
    public Result<Order> update(@RequestBody Order order) {
        return new Result<>(ResultStatus.SUCCESS.getCode(),
                ResultStatus.SUCCESS.getMessage(),
                this.orderService.update(order));
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @DeleteMapping("delete/{id}")
    public Result<Boolean> deleteById(@PathVariable("id") Integer id) {
        return new Result<>(ResultStatus.SUCCESS.getCode(),
                ResultStatus.SUCCESS.getMessage(),
                this.orderService.deleteById(id));
    }

    /**
     * 通过会员Id查询该会员的订单，
     * @param mid 会员ID
     * @return LIst
     */
    @GetMapping("queryOrderByMid/{mid}")
    public Result<List<Order>> queryOrderByMid(@PathVariable("mid") Integer mid) {
        return new Result<>(ResultStatus.SUCCESS.getCode(),
                ResultStatus.SUCCESS.getMessage(),
                this.orderService.queryOrderByMid(mid));
    }
}